// @ts-nocheck


import { useEffect, useRef } from 'react';
import Map from "@geoscene/core/Map.js";
import MapView from "@geoscene/core/views/MapView.js";
import FeatureLayer from "@geoscene/core/layers/FeatureLayer.js";
import WebMap from "@geoscene/core/WebMap.js";
import geosceneConfig from "@geoscene/core/config";
import IdentityManager from "@geoscene/core/identity/IdentityManager";
import ServerInfo from "@geoscene/core/identity/ServerInfo";

import './index.less'


export default function Test() {
    // 创建一个 ref 来指向地图容器 DOM 元素
    const tokenRef = useRef('');

    useEffect(() => {
        silentSignIn();
    }, []);


    // 静默登录函数
    async function silentSignIn() {
        // 配置服务器信息
        const serverUrl = "https://cim.szssscri.com/portal"; // 门户地址
        const tokenServiceUrl = `https://cim.szssscri.com/portal/sharing/rest/generateToken`; // Token服务端点
        const serverInfo = new ServerInfo({
            server: serverUrl,
            tokenServiceUrl: tokenServiceUrl,
            hasPortal: true,
        });

        // 用户凭证
        const userInfo = {
            username: "ZJDSF2024", // 账号
            password: "PQZSsxH9"  // 密码
        };

        try {
            // 1. 生成令牌
            const tokenResponse = await IdentityManager.generateToken(serverInfo, userInfo);
            // console.log("Token 获取成功:", tokenResponse.token);

            // 2. 注册令牌
            IdentityManager.registerToken({
                server: 'https://cim.szssscri.com/portal/sharing/rest', // https://cim.szssscri.com/sharing/rest
                token: tokenResponse.token,
                ssl: true,
                expires: tokenResponse.expires
            });
            // console.log("Token 注册成功");

            tokenRef.current = tokenResponse.token;

            const credential = await IdentityManager.getCredential("https://cim.szssscri.com/portal"); // https://cim.szssscri.com
            // console.log("凭证获取成功", credential);

            // 3. 登录成功后，初始化地图并加载安全图层
            initializeMapWithSecureLayers();

        } catch (error) {
            console.error("静默登录失败:", error);
            // 在此处添加友好的错误提示逻辑
        }
    }

    // 初始化地图和加载安全图层的函数
    function initializeMapWithSecureLayers() {

        const map = new Map({
            basemap: "tianditu-image"
        });

        // geosceneConfig.portalUrl = "https://cim.szssscri.com/portal";
        // const map = new WebMap({
        //     portalItem: { // autocasts as new PortalItem()
        //         id: "ee17fd9468aa4a94a17c653e2a37ef98"
        //     }
        // });

        const view = new MapView({
            container: "test-gis-viewer",
            map: map,
            // center: [114.92757617235301, 22.739862039229976],
            // zoom: 13
        });

        const testLayer = new FeatureLayer({
            url: "https://cim.szssscri.com/server/rest/services/Hosted/%E5%B9%B3%E5%AE%89%E6%B7%B1%E6%B1%95%E6%91%84%E5%83%8F%E5%A4%B4%E7%82%B9%E4%BD%8D%E5%88%86%E5%B8%8320240711/FeatureServer/0?f=json" + `&token=${tokenRef.current}`,
        });
        map.add(testLayer, 0);

        // const testLayer = new FeatureLayer({
        //     url: "https://cim.szssscri.com/server/rest/services/Hosted/合作区自主供应用地_20231010/FeatureServer"
        // });
        // map.add(testLayer, 0);

        //Trailheads feature layer (points)
        // const trailheadsLayer = new FeatureLayer({
        //     url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"
        // });
        // map.add(trailheadsLayer);

        // //Trails feature layer (lines)
        // const trailsLayer = new FeatureLayer({
        //     url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0"
        // });
        // map.add(trailsLayer, 0);

        // // Parks and open spaces (polygons)
        // const parksLayer = new FeatureLayer({
        //     url: "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0"
        // });
        // map.add(parksLayer, 0);



        view.when(() => {
            console.log("Map is loaded");
        })

        view.whenLayerView(testLayer).then((layerView) => {
            view.goTo(testLayer.fullExtent)
        });
    }

    return (
        <div className="test-page">
            <div id="test-gis-viewer"></div>
        </div>
    )
}
